package com.cgt.xiaotao.promotion.controller;

import com.cgt.common.domain.dto.PageDTO;
import com.cgt.common.utils.R;
import com.cgt.xiaotao.promotion.domain.dto.CouponFormDTO;
import com.cgt.xiaotao.promotion.domain.dto.CouponIssueFormDTO;
import com.cgt.xiaotao.promotion.domain.query.CouponQuery;
import com.cgt.xiaotao.promotion.domain.vo.CouponDetailVO;
import com.cgt.xiaotao.promotion.domain.vo.CouponPageVO;
import com.cgt.xiaotao.promotion.domain.vo.CouponVO;
import com.cgt.xiaotao.promotion.service.ICouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "优惠券相关接口-管理端")
@RestController
@RequestMapping("/admin/cou")
@RequiredArgsConstructor
public class CouponAdminController {

    private final ICouponService couponService;

    @ApiOperation("新增优惠券")
    @PostMapping("/add")
    public R<String> saveCoupon(@RequestBody @Validated CouponFormDTO dto) {
        try {
            couponService.saveCoupon(dto);
            return R.ok("新增优惠券成功！");
        } catch (Exception e) {
            return R.fail("新增优惠券失败！");
        }
    }

    @ApiOperation("分页查询优惠券接口-管理端")
    @PostMapping("/page")
    public R<PageDTO<CouponPageVO>> queryCouponByPage(@RequestBody CouponQuery query) {
        try {
            PageDTO<CouponPageVO> couponPageVOPageDTO = couponService.queryCouponByPage(query);
            return R.ok(couponPageVOPageDTO);
        } catch (Exception e) {
            return R.fail("查询优惠券列表失败！");
        }
    }

    @ApiOperation("修改优惠券")
    @PostMapping("/update/{id}")
    public R<String> updateCoupon(@PathVariable Long id, @RequestBody @Validated CouponFormDTO dto) {
        try {
            couponService.updateCoupon(id, dto);
            return R.ok("修改优惠券成功！");
        } catch (Exception e) {
            return R.fail("修改优惠券失败！");
        }
    }

    @ApiOperation("删除优惠券")
    @DeleteMapping("/delete/{id}")
    public R<String> deleteById(@PathVariable Long id, @RequestBody @Validated CouponFormDTO dto) {
        try {
            couponService.deleteById(id, dto);
            return R.ok("删除优惠券成功！");
        } catch (Exception e) {
            return R.fail("删除优惠券失败！");
        }
    }

    @ApiOperation("发放优惠券接口")
    @PutMapping("/{id}/issue")
    public R<String> issueCoupon(@PathVariable Long id, @RequestBody @Validated CouponIssueFormDTO dto) {
        try {
            couponService.issueCoupon(id, dto);
            return R.ok("发放优惠券成功！");
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }

    }

    @ApiOperation("根据id查询优惠券接口")
    @GetMapping("/{id}")
    public R<CouponDetailVO> queryCouponById(@PathVariable("id") Long id) {
        try {
            return R.ok(couponService.queryCouponById(id));
        } catch (Exception e) {
            return R.fail("查询优惠券失败！");
        }
    }

    @ApiOperation("查询发放中的优惠券列表")
    @GetMapping("/list")
    public R<List<CouponVO>> queryIssuingCoupons() {
        try {
            return R.ok(couponService.queryIssuingCoupons());
        } catch (Exception e) {
            return R.fail("查询优惠券列表失败！");
        }
    }

}
